define(['jquery', 'bootstrap', 'backend', 'table', 'form'],function($, undefined, Backend, Table, Form){
    var Controller = {
        index:function(){
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/coupon/index',
                    add_url: 'coupon/coupon/add',
                    edit_url: 'coupon/coupon/edit',
                    del_url: 'coupon/coupon/del',
                    multi_url: 'coupon/coupon/multi',
                    table: 'coupon', // 表名
                }
            });

            // 获取到table标签的id
            var table = $("#table")
            // 初始化表格
            table.bootstrapTable({
                // 当访问列表发起ajax请求数据
                url:$.fn.bootstrapTable.defaults.extend.index_url,
                // 主键
                pk:'id',
                // 设置默认排序
                sortName:'createtime',
                queryParams: function (params) {
                    let filter = params.filter ? JSON.parse(params.filter) : {};
                    // 找到当前激活的选项卡，获取 data-value
                    let status = $(".nav-tabs .active a").attr("data-value");
            
                    if (status && status !== "3") { // 这里假设 "3" 代表 "全部"
                        filter.status = status; // 传递 status
                    } else {
                        delete filter.status; // 如果是 "全部"，不传递 status
                    }
            
                    params.filter = JSON.stringify(filter);
                    return params;
                },
                sortOrder:'desc',
                columns:[
                    [
                        {checkbox: true},
                        {field:'id',title:__('Id'),sortable: true, operate: false},
                        {field:'title',title:__('Ctitle'), operate: 'LIKE'},
                        {field: 'thumb_text', title: __('Image'), formatter: Table.api.formatter.image, operate: false},
                        {field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'endtime', title: __('Endtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                        {field:'rate',title:__('Crate'), operate: false},
                        {field:'total',title:__('Ctotal')},
                        {field: 'status', title: __('Cstatus'), searchList: {"1": __('活动进行中'), "0": __('活动已结束')}, formatter: Table.api.formatter.status},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        // 添加
        add:function()
        {
            Controller.api.bindevent();
        },
        edit:function()
        {
            Controller.api.bindevent();
        },
        del:function()
        {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    }


    return Controller
})